#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define per(i, a, b) for (int i = (a); i >= (b); i--)
#define endl '\n'
const int N = 2e5 + 5;

int n, q, m;
int a[N];
int sum[N << 2], ktag[N << 2], dtag[N << 2];

void pushup(int p) { sum[p] = (sum[p << 1] + sum[p << 1 | 1]) % m; }
void addtag(int k, int d, int p, int l, int r) {
  ktag[p] = 1ll * k * ktag[p] % m;
  dtag[p] = (1ll * k * dtag[p] + d) % m;
  sum[p] = (1ll * k * sum[p] + 1ll * (r + 1 - l) * d) % m;
}
void pushdown(int p, int l, int r) {
  if (ktag[p] != 1 || dtag[p] != 0) {
    int mid = (l + r) >> 1;
    addtag(ktag[p], dtag[p], p << 1, l, mid);
    addtag(ktag[p], dtag[p], p << 1 | 1, mid + 1, r);
    ktag[p] = 1, dtag[p] = 0;
  }
}
void build(int p, int l, int r) {
  ktag[p] = 1, dtag[p] = 0;
  if (l == r) {
    sum[p] = a[l] % m;
    return;
  }
  int mid = (l + r) >> 1;
  build(p << 1, l, mid);
  build(p << 1 | 1, mid + 1, r);
  pushup(p);
}
void update(int L, int R, int k, int d, int p, int l, int r) {
  if (L <= l && R >= r) {
    addtag(k, d, p, l, r);
    return;
  }
  pushdown(p, l, r);
  int mid = (l + r) >> 1;
  if (L <= mid) update(L, R, k, d, p << 1, l, mid);
  if (R > mid) update(L, R, k, d, p << 1 | 1, mid + 1, r);
  pushup(p);
}
int query(int L, int R, int p, int l, int r) {
  if (L <= l && R >= r) return sum[p];
  pushdown(p, l, r);
  int mid = (l + r) >> 1;
  int res = 0;
  if (L <= mid) res = (res + query(L, R, p << 1, l, mid)) % m;
  if (R > mid) res = (res + query(L, R, p << 1 | 1, mid + 1, r)) % m;
  return res;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  rep(i, 1, n) cin >> a[i];
  build(1, 1, n);
  cin >> q;
  while (q--) {
    int op, x, y, k;
    cin >> op >> x >> y;
    if (op != 3) cin >> k;
    if (op == 1) update(x, y, k % m, 0, 1, 1, n);
    if (op == 2) update(x, y, 1, k % m, 1, 1, n);
    if (op == 3) cout << query(x, y, 1, 1, n) << endl;
  }
  return 0;
}